﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.Calculation
{
    public partial class Reallocation : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert;

        protected void Page_Load(object sender, EventArgs e)
        {

        }



        //点击查询作业中心，显示当前查询条件下的基本生产性作业中心
        protected void Button_QueryCenter_Click(object sender, EventArgs e)
        {
            if (TextBox_QueryCenterName.Text == "")
            {
                alert = "<script>alert('请输入作业中心筛选条件。');</script>";
                return;
            
            }

            string Querystring = TextBox_QueryCenterName.Text;
            var CenterlistQuery = from a in db.TABC_JS_CostCenter
                                  where a.作业中心名称.Contains(Querystring)
                                  && new string[] {"BF","BZ","BH","X","M","F","A"}.Contains(a.类型)
                                  select new
                                  {
                                      a.作业中心ID,
                                      作业中心名称 = (a.作业中心名称.Length > 20 ? a.作业中心名称.Substring(0, 19) + "..." : a.作业中心名称)
                                  };

            GridView_CostCenter.DataSource = CenterlistQuery;
            GridView_CostCenter.DataBind();
        }

        //选择当前作业中心，显示对应的产品成本账户。
        protected void Button_CenterSelect_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.ToString() == "")
            {
                return;

            }

            int CenterId = Convert.ToInt32(e.CommandArgument.ToString());

            HiddenField_CenterId.Value =  e.CommandArgument.ToString();

            var CenterName = from a in db.TABC_JS_CostCenter
                             where a.作业中心ID == CenterId
                             select a.作业中心名称;

            Label_CenterName.Text = "当前作业中心：" + CenterName.Single().ToString();

            MyDataBinding(CenterId);


        }

        private void MyDataBinding(int CenterId)
        {

            var MyProductIdQuery = from a in db.TABC_DM_ReAllocation
                                   join b in db.TABC_JS_Product on a.产品ID equals b.产品账户ID

                                   into temps
                                   from temp in temps.DefaultIfEmpty()
                                   where a.作业中心ID == CenterId
                                   select new

                                   {
                                       成本账户ID = a.产品ID,
                                       成本账户名称 = (temp == null ? "" : temp.产品账户名称.Substring(0, 9)),
                                       选择 = false

                                   };
            GridView_SelectProduct.DataSource = MyProductIdQuery;
            GridView_SelectProduct.DataBind();
        
        
        }

        //已选成本账户ID中，将勾打上。
        protected void Button_Selected_All_Click(object sender, EventArgs e)
        {
            if (GridView_SelectProduct.Rows.Count== 0)
            {
                return;
            }

            for (int i = 0; i < GridView_SelectProduct.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_SelectProduct.Rows[i];
                ((CheckBox)myrows.Cells[2].FindControl("CheckBox_Selected")).Checked = true;

            }

        }

        //已选成本账户ID中，反选。
        protected void Button_Selected_UnAll_Click(object sender, EventArgs e)
        {
            if (GridView_SelectProduct.Rows.Count == 0)
            {
                return;
            }

            for (int i = 0; i < GridView_SelectProduct.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_SelectProduct.Rows[i];
                ((CheckBox)myrows.Cells[2].FindControl("CheckBox_Selected")).Checked = !((CheckBox)myrows.Cells[2].FindControl("CheckBox_Selected")).Checked;

            }
        }

        //备选成本账户ID中，将勾打上。
        protected void Button_UnSelected_All_Click(object sender, EventArgs e)
        {
            if (GridView_UnselectProduct.Rows.Count == 0)
            {
                return;
            }

            for (int i = 0; i < GridView_UnselectProduct.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_UnselectProduct.Rows[i];
                ((CheckBox)myrows.Cells[2].FindControl("CheckBox_UnSelected")).Checked = true;

            }
        }

        //备选成本账户ID中，反选。
        protected void Button_UnSelected_Unall_Click(object sender, EventArgs e)
        {


            if (GridView_UnselectProduct.Rows.Count == 0)
            {
                return;
            }

            for (int i = 0; i < GridView_UnselectProduct.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_UnselectProduct.Rows[i];
                ((CheckBox)myrows.Cells[2].FindControl("CheckBox_UnSelected")).Checked = !((CheckBox)myrows.Cells[2].FindControl("CheckBox_UnSelected")).Checked;

            }
        }

        protected void Button_Selected_Del_Click(object sender, EventArgs e)
        {
            int CenterId = Convert.ToInt32(HiddenField_CenterId.Value);


            if (GridView_SelectProduct.Rows.Count == 0)
            {
                return;
            }

            for (int i = 0; i < GridView_SelectProduct.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_SelectProduct.Rows[i];
                if (((CheckBox)myrows.Cells[2].FindControl("CheckBox_Selected")).Checked == true)
                { 
                    string DelProductId = myrows.Cells[0].Text;
                    var DelRows = from a in db.TABC_DM_ReAllocation
                                 where a.产品ID == DelProductId
                                 && a.作业中心ID == CenterId
                                 select a;

                    if (DelRows.Count() == 1)
                    {
                       var  DelRow = DelRows.Single();
                        db.TABC_DM_ReAllocation.DeleteOnSubmit(DelRow);
                        db.SubmitChanges();

                    }

                
                }

            }

            MyDataBinding(CenterId);

        }


        //根据查询条件，筛选备选作业中心。
        protected void Button_QueryUnselectedProductId_Click(object sender, EventArgs e)
        {
            if (TextBox_QueryUnselectedProductDId.Text == "")
            {
                alert = "<script>alert('请输入成本账户ID筛选条件。');</script>";
                return;

            }

            if (HiddenField_CenterId.Value.ToString() == "")
            {
                return;
            }

              int CenterId = Convert.ToInt32(HiddenField_CenterId.Value);

            string Querystring = TextBox_QueryUnselectedProductDId.Text;
            var CenterlistQuery = from c in db.TABC_JS_Product
                                  where ((from a in db.TABC_JS_Product
                                        where a.产品账户ID.StartsWith(Querystring)
                                        select a.产品账户ID).Except(from b in db.TABC_DM_ReAllocation
                                                                where b.作业中心ID == CenterId
                                                                select b.产品ID)).Contains(c.产品账户ID)

                                  select new
                                  {
                                      成本账户ID = c.产品账户ID,
                                      成本账户名称 = (c.产品账户名称.Length > 20 ? c.产品账户名称.Substring(0, 19)+"..." : c.产品账户名称),
                                      选择 = false
                                  };

            GridView_UnselectProduct.DataSource = CenterlistQuery;
            GridView_UnselectProduct.DataBind();
        }


        //将当前产品账户ID插入当前作业中心中。
        protected void Button_Unselected_Save_Click(object sender, EventArgs e)
        {

            if (HiddenField_CenterId.Value.ToString() == "")
            {
                return;
            }

            int CenterId = Convert.ToInt32(HiddenField_CenterId.Value);

            for (int i = 0; i < GridView_UnselectProduct.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_UnselectProduct.Rows[i];
                if (((CheckBox)myrows.Cells[2].FindControl("CheckBox_UnSelected")).Checked == true)
                {
                    string ProductId = myrows.Cells[0].Text;
                    var ExistRows = from a in db.TABC_DM_ReAllocation
                                  where a.产品ID == ProductId
                                  && a.作业中心ID == CenterId
                                  select a;

                    if (ExistRows.Count() == 0)
                    {
                        TABC_DM_ReAllocation AddRow = new TABC_DM_ReAllocation();
                        AddRow.产品ID = ProductId;
                        AddRow.作业中心ID = CenterId;


                        db.TABC_DM_ReAllocation.InsertOnSubmit(AddRow);
                        db.SubmitChanges();

                    }
                    else 
                    {
                    
                    
                    }


                }

            }

            alert = "<script>alert('数据提交成功');</script>";
            MyDataBinding(CenterId);



        }

        protected void Button_LoadToAp_Click(object sender, EventArgs e)
        {
            //刷新当前会计期间的成本重分配设置。
         
            string CurrentAccountPeriod = Application["accountPeriod"].ToString();
            try 
            {
                db.导入重分配设置(CurrentAccountPeriod);
                alert = "<script>alert('数据刷新成功');</script>";
            }
            catch
            {

                alert = "<script>alert('数据刷新失败');</script>";
            }
        }

       
    }
}
